﻿using IOP.ISEA;
using IOP.ISEA.Generator;
using IOP.ISEA.OBJ;
using IOP.SgrA;
using IOP.SgrA.SilkNet.Vulkan;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Threading.Tasks;

namespace VkSample81A
{
    class Program
    {
        static async Task Main(string[] args)
        {
            await CreateISEA(true);
            var h = Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((config) =>
                {
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) config.AddVulkanJsonFile("Vulkan.json");
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) config.AddVulkanJsonFile("Vulkan-Linux.json");
                    else throw new NotSupportedException("Not supported Platform");
                })
                .ConfigureSgrAHost((sgra) =>
                {
                    sgra.AddVulkanEnvironmental();
                    sgra.UseStartup<Startup>();
                }).ConfigureLogging((logger) => logger.AddConsole()); ;
            await h.RunConsoleAsync();
        }

        static async Task CreateISEA(bool isNeed)
        {
            if (!isNeed) return;
            var basePath = AppContext.BaseDirectory;
            var assetsPath = Path.Combine(basePath, "Assets", "generator.json");
            using var stream = File.OpenRead(assetsPath);
            ISEADocument isea = await ISEAGenerator.GenerateDocument(stream);
            if (isea.TryGetNodeFromName("CFT", out RenderObjectNode cftRender))
            {
                cftRender.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 25 * MathF.PI / 180.0f);
                cftRender.Position = new Vector3(-10, 0, 0); cftRender.Scale = new Vector3(1.5f, 1.5f, 1.5f);
            }
            if(isea.TryGetNodeFromName("CH", out RenderObjectNode chRender))
            {
                chRender.Position = new Vector3(0, 0, 10); chRender.Scale = new Vector3(1.5f, 1.5f, 1.5f);
                chRender.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 25 * MathF.PI / 180.0f);
            }
            if(isea.TryGetNodeFromName("PM", out RenderObjectNode pmRender))
            {
                pmRender.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 25 * MathF.PI / 180.0f);
                pmRender.Position = Vector3.Zero; pmRender.Scale = new Vector3(1, 1, 1);
            }
            if(isea.TryGetNodeFromName("QT", out RenderObjectNode qtRender))
            {
                qtRender.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 25 * MathF.PI / 180.0f);
                qtRender.Position = new Vector3(10, 0, 0); qtRender.Scale = new Vector3(1.5f, 1.5f, 1.5f);
            }
            if(isea.TryGetNodeFromName("YH", out RenderObjectNode yhRender))
            {
                yhRender.Rotation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), 25 * MathF.PI / 180.0f);
                yhRender.Position = new Vector3(0, 0, -10); yhRender.Scale = new Vector3(1.5f, 1.5f, 1.5f);
            }
            await isea.Save(basePath, "test81");
        }
    }
}
